package com.huixue.cn.ztej.api.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.util.StringUtil;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.TQuestionsInfo;
import com.huixue.cn.ztej.bean.TTudentInfo;
import com.huixue.cn.ztej.service.TQuestionsService;
import com.huixue.cn.ztej.service.TTudentService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.JacksonUtil;


@Controller
@RequestMapping(value = "/api/torder")
public class ApiQuestionController {
	
	@Autowired(required = true)
	private TTudentService sTudentService;
	
	@Autowired(required = true)
	private TQuestionsService tQuestionsService;
	
	
	@RequestMapping(value = "/saveQuestions",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	@ResponseBody
	public String saveQuestions(@RequestBody TQuestionsInfo info,RedirectAttributes attrs ) {
		Results results = null;
		if(info.getStudentId()!=null){
			results = new Results(API_RETURN_STATUS.PARA_ERROR.value(), "参数必填！");
			return JSON.toJSONString(results);
		}
		TTudentInfo tti =  sTudentService.getInfo(info.getStudentId());
		if(tti==null){
			results = new Results(API_RETURN_STATUS.USERNOEXIST_ERROR.value(), "用户不存在！");
			return JSON.toJSONString(results);
		}
		info.setSchoolId(tti.getSchoolId());
		info.setGradeId(tti.getGradeId());
		info.setClassId(tti.getClassId());
		info.setStatus(1);
		info.setCreateTime(new Date());
		tQuestionsService.save(info);
		results = new Results(API_RETURN_STATUS.NORMAL.value(), "保存成功！");
		return JSON.toJSONString(results);
	}
	
	@RequestMapping(value = "/findQuestionsNumber/{openId}",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String findQuestionsNumber(@PathVariable ("openId")String openId,RedirectAttributes attrs ) {
		Results results = null;
		if(StringUtil.isEmpty(openId)){
			results = new Results(API_RETURN_STATUS.PARA_ERROR.value(), "openId为空！");
			return JSON.toJSONString(results);
		}
		TTudentInfo tti =  sTudentService.findbyOpenId(openId);
		if(tti==null){
			results = new Results("3","用户不存在","");
			return new JacksonUtil().getJson(results);
		}
		Map<String,Object> resultmap = new HashMap<String,Object>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("studentId",tti.getId() );
		Long questionsNumber  = tQuestionsService.findQuestionsNumber(map);
		Long pushquestionsNumber  = tQuestionsService.findPushQuestionsNumber(tti.getId());
		resultmap.put("questionsNumber", questionsNumber);
		resultmap.put("pushquestionsNumber", pushquestionsNumber);
		results = new Results(API_RETURN_STATUS.NORMAL.value(),"查询成功");
		results.setData(resultmap);
		return JSON.toJSONString(results);
	}
	
	@RequestMapping(value = "/findSubjectQuestionsNumber/{openId}",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String findSubjectQuestionsNumber(@PathVariable ("openId")String openId,RedirectAttributes attrs ) {
		Results results = null;
		if(StringUtil.isEmpty(openId)){
			results = new Results(API_RETURN_STATUS.PARA_ERROR.value(), "openId为空！");
			return JSON.toJSONString(results);
		}
		TTudentInfo tti =  sTudentService.findbyOpenId(openId);
		if(tti==null){
			results = new Results("3","用户不存在","");
			return new JacksonUtil().getJson(results);
		}
		Map<String,Object> resultmap = new HashMap<String,Object>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("studentId",tti.getId() );
		map.put("subjectId", 1);
		Long YWquestionsNumber  = tQuestionsService.findQuestionsNumber(map);
		map.put("subjectId", 2);
		Long SXquestionsNumber  = tQuestionsService.findQuestionsNumber(map);
		map.put("subjectId", 3);
		Long YYquestionsNumber  = tQuestionsService.findQuestionsNumber(map);
		resultmap.put("1", YWquestionsNumber);
		resultmap.put("2", SXquestionsNumber);
		resultmap.put("3", YYquestionsNumber);
		results = new Results(API_RETURN_STATUS.NORMAL.value(),"查询成功");
		results.setData(resultmap);
		return JSON.toJSONString(results);
	}

}

